package com.yanchangapp.ui.presenter;

import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.MailType;
import com.yanchangapp.model.response.MailDetailResponse;
import com.yanchangapp.ui.view.MailDetailAtView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019/6/28.
 * description: 邮件详情p层
 */
public class MailDetailAtPresenter extends BasePresenter<MailDetailAtView> {
    public MailDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryMailDetailInfo(int maidID){
        HeadApiRetrofit.getInstance().queryMailDetailInfo(maidID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MailDetailResponse>() {
                    @Override
                    public void onNext(MailDetailResponse mailDetailResponse) {
                        if (mailDetailResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().queryMailDetailSuccess(mailDetailResponse.getData());
                            }
                        }else {
                            if (getView() != null){
                                getView().queryMailDetailFailure(mailDetailResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryMailDetailFailure("获取邮件详情失败");
                        }
                    }
                });
    }

    /**
     * 完全删除...
     * @param currentID
     */
    public void actualDelete(int currentID){
        List<Integer> selectLists = new ArrayList<>();
        selectLists.add(currentID);
        HeadApiRetrofit.getInstance().actualDeleteMail(selectLists)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().deleteSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().deleteFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().deleteFailure("删除失败");
                        }
                    }
                });

    }

    /**
     * 移动邮件到已删除
     * @param currentID
     */
    public void moveToDelete(int currentID){
        List<Integer> selectLists = new ArrayList<>();
        selectLists.add(currentID);
        HeadApiRetrofit.getInstance().moveMailsToTargetMails(MailType.TRASH.getMailDetail().getNetState(),selectLists)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().deleteSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().deleteFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().deleteFailure("删除失败");
                        }
                    }
                });
    }

}
